package com.mw.sotest;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.blankj.utilcode.util.PermissionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import cn.zyyl.ai.face.FaceDetector2;
import cn.zyyl.ai.face.SeetaImageData;
import cn.zyyl.ai.face.SeetaPointsFace;
import cn.zyyl.ai.face.SeetaRect;


public class FirstFragment extends Fragment {

    private TextView textView;

    private FaceDetector2 mFaceDetect = FaceDetector2.getInstance();

    private static String facesPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/1#D^#$DT##R&%.dat";


    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        boolean granted = PermissionUtils.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (!granted) {
            PermissionUtils.permission(Manifest.permission.WRITE_EXTERNAL_STORAGE).callback(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {

                }

                @Override
                public void onDenied() {
                    Log.d("xxx", "permission is denied");
                }
            }).request();
        } else {

        }


        mFaceDetect.loadEngine(facesPath);


//        String[] imgs = {
//                "01.jpg","02.jpg","03.jpg","04.jpg","05.jpg","06.jpg","07.jpg","08.jpg","09.jpg","10.jpg",
//                "11.jpg","12.jpg","13.jpg","14.jpg","15.jpg","16.jpg","17.jpg","18.jpg","19.jpg","20.jpg",
//                "21.jpg","22.jpg","23.jpg","24.jpg","25.jpg","26.jpg","27.jpg","28.jpg","29.jpg","30.jpg",
//                "31.jpg","32.jpg","33.jpg","34.jpg","35.jpg","36.jpg","37.jpg","38.jpg","39.jpg","40.jpg",
//                "41.jpg","42.jpg","43.jpg","44.jpg","45.jpg","46.jpg","47.jpg","48.jpg","49.jpg","50.jpg",
//                "51.jpg","52.jpg","53.jpg","54.jpg"
//        };

        String[] imgs = {
                "01.jpg","02.jpg","03.jpg"
        };
            for(String img : imgs){
                run(img);
            }

            //人脸查找
            for(String img : imgs){
                Recognizer(img);
            }

            textView = view.findViewById(R.id.textview_first);

            view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    NavHostFragment.findNavController(FirstFragment.this)
                            .navigate(R.id.action_FirstFragment_to_SecondFragment);
                }
            });


    }


    public void Recognizer(String img){
        InputStream inputStream = null;
        try {
            inputStream = getActivity().getAssets().open(img);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (inputStream != null) {
            Bitmap _bitmap = BitmapFactory.decodeStream(inputStream);

            //byte[] imgBytes = ImageUtils.bitmap2Bytes(_bitmap);

            byte[] imgBytes = mFaceDetect.getPixelsBGR(_bitmap);

            SeetaImageData seetaImageData = new SeetaImageData(_bitmap.getWidth(), _bitmap.getHeight(), 3);
            seetaImageData.data = imgBytes;
            Log.d("xxx Recognizer", "data : " + seetaImageData.toString());

            int result = mFaceDetect.Recognizer(seetaImageData.width,seetaImageData.height,
                    seetaImageData.channels,seetaImageData.data.length,seetaImageData.data
            );

            Log.d("xxx Recognizer", "search face index result : " + result);


            //质量检测
            float rf = mFaceDetect.QA(seetaImageData.width,seetaImageData.height,
                    seetaImageData.channels,seetaImageData.data.length,seetaImageData.data);
            Log.d("qqqqqqqqqqqqq QA", "QA : " + rf);

            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void run(String img){
        InputStream inputStream = null;
        try {
            inputStream = getActivity().getAssets().open(img);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (inputStream != null) {
            Bitmap _bitmap = BitmapFactory.decodeStream(inputStream);

            //byte[] imgBytes = ImageUtils.bitmap2Bytes(_bitmap);

            byte[] imgBytes = mFaceDetect.getPixelsBGR(_bitmap);

            SeetaImageData seetaImageData = new SeetaImageData(_bitmap.getWidth(), _bitmap.getHeight(), 3);
            seetaImageData.data = imgBytes;
            Log.d("xxx", "data : " + seetaImageData.toString());


            //textView.setText("JNI:[" + faceDetector2.test(9, 10) + "] - " + faceDetector2.testStr(rootpath));
            //SeetaRect[] rects = mFaceDetect.Detect(seetaImageData);
            SeetaRect rect1 = new SeetaRect();
            SeetaRect rect2 = new SeetaRect();
            SeetaPointsFace seetaPointsFace = new SeetaPointsFace();
            //for(int i=0;i<100;i++){
            int result = mFaceDetect.Detect(seetaImageData.width,seetaImageData.height,
                    seetaImageData.channels,seetaImageData.data.length,seetaImageData.data,
                    rect1,rect2,seetaPointsFace
            );

            Log.d("xxx", "search face index result : " + result);
            //}


            //for(int i=0;i<100;i++) {
            int regResult = mFaceDetect.Register(seetaImageData.width, seetaImageData.height,
                    seetaImageData.channels, seetaImageData.data.length, seetaImageData.data, facesPath);
            // }
            if(rect1 != null && rect1.x > 0){
                Log.d("xxx", "rect1 data : " + rect1.x + "  " + rect1.y + "  " + rect1.width + "  " + rect1.height);
            }else{
                Log.d("else:xxx", "rect1 data : " + rect1.x + "  " + rect1.y + "  " + rect1.width + "  " + rect1.height);
            }
            if(rect1 != null && rect2.x > 0){
                Log.d("xxx", "rect2 data : " + rect2.x + "  " + rect2.y + "  " + rect2.width + "  " + rect2.height);
            }else{
                Log.d("else:xxx", "rect2 data : " + rect2.x + "  " + rect2.y + "  " + rect2.width + "  " + rect2.height);
            }


            Log.d("xxxxxxxxxx", "version : " + mFaceDetect.version());
            Log.d("xxxxxxxxxx", "facesCount : " + mFaceDetect.facesCount());
            long[] ids = mFaceDetect.DBIds();
            if(null != ids){
                int l = ids.length;
                Log.d("xxxxxxxxxx", "db size : " + l);
                if(l>0){
                    for(int i=0;i<l;i++){
                        Log.d("xxxxxxxxxx", "db ids : " + ids[i]);
                    }


                    boolean _r = mFaceDetect.deleteFace(0l,facesPath);
                    Log.d("xxxxxxxxxx", "deleteFace result : " + _r);
                    Log.d("xxxxxxxxxx", "facesCount : " + mFaceDetect.facesCount());
                    ids = mFaceDetect.DBIds();
                    l = ids.length;
                    Log.d("xxxxxxxxxx", "db size : " + l);
                    if(l>0){
                        for(int i=0;i<l;i++){
                            Log.d("xxxxxxxxxx", "db ids : " + ids[i]);
                        }
                    }

                }
            }


            Log.d("xxxxxxxxxx", "facesCount : " + mFaceDetect.facesCount());
            Log.d("xxxxxxxxxx", "facesCount : " + mFaceDetect.facesCount());

            if(seetaPointsFace != null && seetaPointsFace.leftEye_X > 0){
                Log.d("xxx", "seetaPointsFace data : " +
                        seetaPointsFace.leftEye_X + "  " + seetaPointsFace.leftEye_Y + "  "
                        + seetaPointsFace.rightEye_X + "  " + seetaPointsFace.rightEye_Y+ "  "
                        + seetaPointsFace.nose_X + "  " + seetaPointsFace.nose_Y+ "  "
                        + seetaPointsFace.leftMouth_X + "  " + seetaPointsFace.leftMouth_Y+ "  "
                        + seetaPointsFace.rightMouth_X + "  " + seetaPointsFace.rightMouth_Y+ "  "

                );
            }else{

            }


            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}